package com.coffeeloversupportlibrary.coffeelover.domain;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.bumptech.glide.Glide;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.explor.ExploreActivity;
import com.coffeeloversupportlibrary.coffeelover.uicompont.NoScrollPage;
import com.coffeeloversupportlibrary.core.base.BaseActivity;
import com.coffeeloversupportlibrary.databinding.ActivityMainBinding;
import com.lzy.ninegrid.NineGridView;



/**
* @date 2021/11/22 13:26
* @params
* @return
* @description TODO   主页面 包括底部导航控件  NoScrollerViewPage  Fragment 容器
*/
public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> implements View.OnClickListener{
    private NoScrollPage containerPage;
    private BottomNavigationBar navigationBar;


    @Override
    public void registerUIChangeCallBack() {
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    /**
    * @date 2021/11/8 10:11
    * @params [savedInstanceState]
    * @return void
    * @description 实现该方法，完成界面的初始化以及初始数据的加载
    */
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initFragment();
        initNavigationBar();

        //初始化九宫格控件所使用的图片加载器
        NineGridView.setImageLoader(new GlideImageLoader());
    }

    private void initNavigationBar(){
        //todo 典型问题：改的时轮播图里面的视图（删除edit 此处时外层页面的底部导航栏？--为什么会产生空引用异常呢？）
        getBinding().tabViewMain.selected();
        getBinding().containerPage.setCurrentItem(0,true);


        getBinding().mainAddBtn.setOnClickListener(this);
        getBinding().tabViewMain.setOnClickListener(this);
        getBinding().tabViewMine.setOnClickListener(this);
        getBinding().tabViewDeal.setOnClickListener(this);
        getBinding().tabViewMsg.setOnClickListener(this);
    }


    /**
    * @date 2021/11/22 13:33
    * @params []
    * @return void
    * @description TODO  初始化相关 Fragment
    */
    private void initFragment(){
        getBinding().containerPage.setAdapter(getViewModel().initFragment(getSupportFragmentManager(),MainActivity.this));
    }


    @Override
    public void onClick(View v) {

        // viewPage 底部导航栏联动
        if(v.equals(getBinding().tabViewMain)){
            getBinding().containerPage.setCurrentItem(0);
            getBinding().tabViewMain.selected();
            getBinding().tabViewMsg.unSelected();
            getBinding().tabViewDeal.unSelected();
            getBinding().tabViewMine.unSelected();

        }else if(v.equals(getBinding().tabViewMine)){
            getBinding().containerPage.setCurrentItem(3);
            getBinding().tabViewMain.unSelected();
            getBinding().tabViewMsg.unSelected();
            getBinding().tabViewDeal.unSelected();
            getBinding().tabViewMine.selected();

        }else if(v.equals(getBinding().tabViewMsg)){
            getBinding().containerPage.setCurrentItem(2);
            getBinding().tabViewMain.unSelected();
            getBinding().tabViewMsg.selected();
            getBinding().tabViewDeal.unSelected();
            getBinding().tabViewMine.unSelected();

        }else if(v.equals(getBinding().tabViewDeal)){
            getBinding().containerPage.setCurrentItem(1);
            getBinding().tabViewMain.unSelected();
            getBinding().tabViewMsg.unSelected();
            getBinding().tabViewDeal.selected();
            getBinding().tabViewMine.unSelected();

        }else if(v.equals(getBinding().mainAddBtn)){
            // TODO: 2021/11/22 : 中间添加按钮启动咖啡厅功能
            startActivity(ExploreActivity.class);

        }
    }


    // TODO: 2021/11/22 :九宫格图片加载器 使用  Glide  此处配置图片加载相关信息
    private class GlideImageLoader implements NineGridView.ImageLoader{

        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            Glide.with(context).load(url).into(imageView);
        }
        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }

}





